package elion.com.theElion.util;

import android.content.res.Resources;
import android.content.res.TypedArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Alex on 2017/5/26.
 * 用于获取drawable数组中的drawable的id
 */

public class ResUtil
{
    public static int[] getImgIdRes(Resources res, int arrID)
    {
        TypedArray draws;
        draws = res.obtainTypedArray(arrID);
        int n = draws.length();
        int[] drawsID = new int[n];
        for(int i = 0 ; i < n;i ++) {
            drawsID[i] = draws.getResourceId(i, 0);
        }
        return drawsID;
    }
    public static String changeToRealName(String type)
    {
        String nt = "";
        switch (type)
        {
            //气象数据
            case "air_temp":
                nt = "空气温度";
                break;
            case "air_hum":
                nt = "空气湿度";
                break;
            case "sun_data":
                nt = "光照强度";
                break;
            case "co2":
                nt = "二氧化碳";
                break;
            case "win_speed":
                nt = "风速";
                break;

            //土壤
            case "soil_temp":
                nt = "土壤温度(0-20CM)";
                break;
            case "soil_temp1":
                nt = "土壤温度(20-40CM)";
                break;
            case "soil_humi":
                nt = "土壤湿度(0-20CM)";
                break;
            case "soil_humi1":
                nt = "土壤湿度(20-40CM)";
                break;
            case "soil_con":
                nt = "电导率(0-20CM)";
                break;
            case "soil_con1":
                nt = "电导率(20-40CM)";
                break;
            case "yanfen":
                nt = "土壤盐分(0-20CM)";
                break;
            case "yanfen1":
                nt = "土壤盐分(20-40CM)";
                break;

            //水位数据
            case "water_level":
                nt = "水平面海拔";
                break;


            default:
                nt = "";
                break;
        }
        return nt;
    }

    public static String getUnit(String type)
    {
        String nt = "";
        switch (type)
        {
            //气象数据
            case "air_temp":
                nt = "℃";
                break;
            case "air_hum":
                nt = "%rh";
                break;
            case "sun_data":
                nt = "KLX";
                break;
            case "co2":
                nt = "PPM";
                break;
            case "win_speed":
                nt = "km/h";
                break;

            //土壤数据
            case "soil_temp":
                nt = "℃";
                break;
            case "soil_temp1":
                nt = "℃";
                break;
            case "soil_humi":
                nt = "%";
                break;
            case "soil_humi1":
                nt = "%";
                break;
            case "soil_con":
                nt = "%";
                break;
            case "soil_con1":
                nt = "%";
                break;
            case "yanfen":
                nt = "mS/cm";
                break;
            case "yanfen1":
                nt = "mS/cm";
                break;

            //水位数据
            case "water_level":
                nt = "m";
                break;

            default:
                nt = "";
                break;
        }
        return nt;
    }

    public static String changeToFieldName(String realName)
    {
        String nt = "";
        switch (realName)
        {
            //气象数据
            case "空气温度":
                nt = "air_temp";
                break;
            case "空气湿度":
                nt = "air_hum";
                break;
            case "光照强度":
                nt = "sun_data";
                break;
            case "二氧化碳":
                nt = "co2";
                break;
            case "风速":
                nt = "win_speed";
                break;

            //土壤
            case "土壤温度(0-20CM)":
                nt = "soil_temp";
                break;
            case "土壤温度(20-40CM)":
                nt = "soil_temp1";
                break;
            case "土壤湿度(0-20CM)":
                nt = "soil_humi";
                break;
            case "土壤湿度(20-40CM)":
                nt = "soil_humi1";
                break;
            case "电导率(0-20CM)":
                nt = "soil_con";
                break;
            case "电导率(20-40CM)":
                nt = "soil_con1";
                break;
            case "土壤盐分(0-20CM)":
                nt = "yanfen";
                break;
            case "土壤盐分(20-40CM)":
                nt = "yanfen1";
                break;

            //水位数据
            case "水平面海拔":
                nt = "water_level";
                break;


            default:
                nt = "";
                break;
        }
        return nt;
    }

    public static List<Map<String, Object>> fromBeanToList(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
    {
        List<Map<String, Object>> params = new ArrayList<>();
        String[] names = ReflectUtil.getFieldsNames(obj);
        int n = names.length;


        for (int i = 0; i < names.length; i++)
        {
            HashMap<String, Object> param = new HashMap<>();
//            param.put("dataImg",);图片
            param.put("dataName",changeToRealName(names[i]));
            if (!changeToRealName(names[i]).equals("")&&!ResUtil.getUnit(names[i]).equals(""))
            {
                BigDecimal bd = new BigDecimal(Double.parseDouble(ReflectUtil.useGetMethod(obj,names[i])));
                param.put("dataValue",  bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            }else
            {
                param.put("dataValue",ReflectUtil.useGetMethod(obj,names[i]));
            }

            //取得相应字段名的数据,这里只取两位数字，防止显示页面出现不够长的bug
            param.put("dataUnit", ResUtil.getUnit(names[i]));//取得字段名的单位

            params.add(param);
        }


        int size = params.size();
        for (int i = 0; i < size; i++)
        {
            Map<String, Object> param;
            param=params.get(i);
            if (param.get("dataName").equals("")&&param.get("dataUnit").equals(""))
            {
                params.remove(i);
                i--;
                size--;
            }
//            else System.out.println(param.get("dataName")+"="+param.get("dataValue")+param.get("dataUnit"));

        }
        return params;
    }

    public static Object jsonMyData(String result,Class objClass) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException//只需要从result中解析出一个Elion_Qixiang对象
    {

        Object obj=objClass.getConstructor().newInstance();
        String[] names = ReflectUtil.getFieldsNames(obj);
        int n = names.length;

        try
        {
            JSONArray array = new JSONArray(result);
            JSONObject jsonObject = new JSONObject();

            for (int i = 0; i < array.length(); i++)
            {
                jsonObject = array.getJSONObject(i);
                for(int j = 0;j < n;j++)
                {
                    ReflectUtil.useSetMethod(obj, names[j],jsonObject.getString(names[j]));
//                    System.out.println(jsonObject.getString(names[j]));
//                    System.out.println(ReflectUtil.useGetMethod(obj,names[j]));
                }
            }


        } catch (JSONException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e)
        {
            e.printStackTrace();
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        } catch (InvocationTargetException e)
        {
            e.printStackTrace();
        }
        return obj;
    }


}
